package com.legal.app.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.legal.app.mapper.FavoriteMapper;
import com.legal.app.domain.Favorite;
import com.legal.app.service.IFavoriteService;
import com.legal.common.core.domain.model.LoginUser;
import com.legal.common.utils.DateUtils;
import com.legal.common.utils.SecurityUtils;
import com.legal.app.domain.vo.FavoriteLawyerVO;
import lombok.extern.slf4j.Slf4j;

/**
 * 收藏Service业务层处理
 * 
 * @author Ale
 * @date 2025-06-21
 */
@Slf4j
@Service
public class FavoriteServiceImpl implements IFavoriteService {
    @Autowired
    private FavoriteMapper favoriteMapper;

    /**
     * 查询收藏
     * 
     * @param id 收藏主键
     * @return 收藏
     */
    @Override
    public Favorite selectFavoriteById(String id) {
        return favoriteMapper.selectFavoriteById(id);
    }

    /**
     * 查询收藏列表
     * 
     * @param favorite 收藏
     * @return 收藏
     */
    @Override
    public List<Favorite> selectFavoriteList(Favorite favorite) {
        return favoriteMapper.selectFavoriteList(favorite);
    }

    /**
     * 新增收藏
     * 
     * @param favorite 收藏
     * @return 结果
     */
    @Override
    public int insertFavorite(Favorite favorite) {
        return favoriteMapper.insertFavorite(favorite);
    }

    /**
     * 修改收藏
     * 
     * @param favorite 收藏
     * @return 结果
     */
    @Override
    public int updateFavorite(Favorite favorite) {
        return favoriteMapper.updateFavorite(favorite);
    }

    /**
     * 批量删除收藏
     * 
     * @param ids 需要删除的收藏主键
     * @return 结果
     */
    @Override
    public int deleteFavoriteByIds(String[] ids) {
        return favoriteMapper.deleteFavoriteByIds(ids);
    }

    /**
     * 删除收藏信息
     * 
     * @param id 收藏主键
     * @return 结果
     */
    @Override
    public int deleteFavoriteById(String id) {
        return favoriteMapper.deleteFavoriteById(id);
    }

    @Override
    public int addFavorite(Long lawyerId) {
        Long userId = SecurityUtils.getUserId();
        log.info("【收藏服务】addFavorite - 开始，userId: {}, lawyerId: {}", userId, lawyerId);

        if (userId == null) {
            log.error("【收藏服务】addFavorite - 无法获取用户ID，用户可能未登录。");
            return 0;
        }

        // 检查是否已经收藏
        Favorite check = favoriteMapper.selectFavoriteByUserIdAndLawyerId(userId, lawyerId);
        if (check != null) {
            log.warn("【收藏服务】addFavorite - 用户 {} 已收藏过律师 {}，无需重复操作。", userId, lawyerId);
            // 如果已经存在，则不进行任何操作，直接返回成功
            return 1;
        }

        Favorite favorite = new Favorite();
        favorite.setUserId(userId);
        favorite.setLawyerId(lawyerId);
        favorite.setCreatedAt(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null) {
            favorite.setCreatedBy(loginUser.getUsername());
        }

        int result = favoriteMapper.insertFavorite(favorite);
        log.info("【收藏服务】addFavorite - 数据库 insertFavorite 操作完成，影响行数: {}", result);
        return result;
    }

    @Override
    public int cancelFavorite(Long lawyerId) {
        Long userId = SecurityUtils.getUserId();
        log.info("【收藏服务】cancelFavorite - 开始, userId: {}, lawyerId: {}", userId, lawyerId);

        if (userId == null) {
            log.error("【收藏服务】cancelFavorite - 无法获取用户ID，用户可能未登录。");
            return 0;
        }

        int result = favoriteMapper.deleteFavoriteByUserIdAndLawyerId(userId, lawyerId);
        log.info("【收藏服务】cancelFavorite - 数据库 deleteFavorite 操作完成，影响行数: {}", result);
        return result;
    }

    @Override
    public List<FavoriteLawyerVO> getFavoriteLawyerList(Long userId) {
        return favoriteMapper.selectFavoriteLawyerList(userId);
    }
}
